package com.groupfifteen.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupfifteen.dao.SysExamManageDao;
import com.groupfifteen.entity.SysExamManageEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service("sysExamManageService")
public class SysExamManageServiceImpl extends ServiceImpl<SysExamManageDao, SysExamManageEntity>
        implements SysExamManageService {
    private final SysExamManageDao examManageMapper;

    private final SysPaperManageService paperService;

    @Override
    public IPage<SysExamManageEntity> QuerySysExamManages(SysExamManageEntity sysExamManageEntity, int pageNo, int pageSize) {
        LambdaQueryWrapper<SysExamManageEntity> query = Wrappers.lambdaQuery();
        query.eq(sysExamManageEntity.getExamCode() != null, SysExamManageEntity::getExamCode, sysExamManageEntity.getExamCode());

        if (pageNo == 0) {
            pageNo = 1;
        }
        if (pageSize == 0) {
            pageSize = 10;
        }

        IPage<SysExamManageEntity> page = new Page<>(pageNo, pageSize);
        return baseMapper.selectPage(page, query);
    }

    @Override
    public Integer AddExam(SysExamManageEntity sysExamManageEntity) {
        return baseMapper.insert(sysExamManageEntity);
    }

    @Override
    public Integer DeleteExam(Integer exam_code) {
        return baseMapper.deleteById(exam_code);
    }

    @Override
    public SysExamManageEntity GetExam(Integer exam_code) {
        return baseMapper.selectById(exam_code);
    }

    @Override
    public Integer UpdateExam(SysExamManageEntity sysExamManageEntity) {
        return baseMapper.updateById(sysExamManageEntity);
    }

    /**
     * 查询所有考试
     *
     * @return 考试列表
     */
    @Override
    public List<SysExamManageEntity> findAll() {
        Page<SysExamManageEntity> examManage = new Page<>(0, 9999);
        List<SysExamManageEntity> examManageList = examManageMapper.findAll(examManage).getRecords();
        setMaxScore(examManageList);
        return examManageList;
    }

    /**
     * 查询考试 分页
     *
     * @param page 分页对象
     * @return 考试列表
     */
    @Override
    public IPage<SysExamManageEntity> findAll(Page<SysExamManageEntity> page) {
        IPage<SysExamManageEntity> iPage = examManageMapper.findAll(page);
        setMaxScore(iPage.getRecords());
        return iPage;
    }

    /**
     * 根据考试编号查询考试信息
     *
     * @param examCode 考试编好
     * @return 考试信息
     */
    @Override
    public SysExamManageEntity findById(Integer examCode) {
        SysExamManageEntity examManage = examManageMapper.findById(examCode);
        examManage.setTotalScore(paperService.getMaxScore(examManage.getPaperId()));
        return examManage;
    }

    /**
     * 根据考试编号删除
     *
     * @param examCode 考试编号
     */
    @Override
    public Integer delete(Integer examCode) {
        return examManageMapper.delete(examCode);
    }

    /**
     * 更新考试信息
     *
     * @param examManage 考试信息
     */
    @Override
    public Integer update(SysExamManageEntity examManage) {
        return examManageMapper.update(examManage);
    }

    /**
     * 添加考试信息
     *
     * @param examManage 考试信息
     */
    @Override
    public Integer add(SysExamManageEntity examManage) {
        return examManageMapper.add(examManage);
    }

    /**
     * 查询最后一条记录的paperId,返回给前端达到自增效果
     *
     * @return 最后一条记录
     */
    @Override
    public SysExamManageEntity findOnlyPaperId() {
        return examManageMapper.findOnlyPaperId();
    }

    /**
     * 设置最高分
     *
     * @param examManageList
     */
    private void setMaxScore(List<SysExamManageEntity> examManageList) {
        for (SysExamManageEntity examManage : examManageList) {
            examManage.setTotalScore(paperService.getMaxScore(examManage.getPaperId()));
        }
    }
}